<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="天数的id" prop="fctDayMealId">
        <el-input
          v-model="queryParams.fctDayMealId"
          placeholder="请输入天数的id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="顺序" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入顺序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:detl:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:detl:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:detl:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:detl:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detlList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" />
      <!-- <el-table-column label="膳食id" align="center" prop="id" /> -->
      <el-table-column label="膳食类型" align="center" prop="dayType">
            <template slot-scope="scope">
              {{(dict.type.food_day_type.find(obj => obj.value == scope.row.dayType).label)}}
            </template>
      </el-table-column>         
      <el-table-column label="症状" align="center" prop="symptomType" >
            <template slot-scope="scope">
              {{(dict.type.sys_symptom_type.find(obj => obj.value == scope.row.symptomType).label)}}
            </template>
      </el-table-column>      
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAddMeals(scope.row)"
            v-hasPermi="['system:detl:edit']"
          >添加菜品</el-button>          
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:detl:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:detl:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改一天膳食食谱详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="膳食id" prop="fct_day_meal_id">
          <el-input v-model="form.fct_day_meal_id" placeholder="请输入id" />
        </el-form-item> -->
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>

        <el-form-item label="膳食类型" prop="dayType">
          <!-- <el-input v-model="form.dayType" placeholder="请输入名称" /> -->
          <el-select v-model="form.dayType" placeholder="请选择">
            <el-option
              v-for="item in dict.type.food_day_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              clearable>
            </el-option>
          </el-select>          
        </el-form-item>

        <el-form-item label="症状" prop="symptomType">
          <!-- <el-input v-model="form.symptomType" placeholder="请输入名称" /> -->
          <el-select v-model="form.symptomType" placeholder="请选择">
            <el-option
              v-for="item in dict.type.sys_symptom_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              clearable>
            </el-option>
          </el-select>           
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
       
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

     <!-- 添加或修改一天膳食食谱详情对话框 -->
    <el-dialog :title="title" :visible.sync="mealopen" width="500px" append-to-body>
     
     
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item>
            <el-button v-if="form.fctMealsVOS && form.fctMealsVOS.length === 0" class="btn"  @click="addMeal" type="primary">添加食物</el-button>
            <div v-for="(item,index) in form.fctMealsVOS" :key="index">
                <el-select
                    :style="{ width: '200px',margin: '10px 0 10px 0' }"
                    v-model="form.fctMealsVOS[index].mealsId"
                    filterable
                    remote
                    reserve-keyword
                    placeholder=""
                    :remote-method="(query) => remoteMethodFront(query, item)"
                    @focus="remoteMethodFront(undefined, item)"
                    @change="getMealName(item)"
                    clearable
                    >
                    <el-option
                      v-for="item in mealList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                    <span style="float: left">{{ item.name }}</span>
                    <img style="float: right; width: 30px; height: 30px;" v-if="item.cover.length>0" :src="'http://health-fcd.oss-cn-hangzhou.aliyuncs.com/' + item.cover[0].url" />
                    </el-option>
                </el-select>
                <!-- <el-input v-model="form.fctMealsVOS[index].mealType"> -->
                
                <el-select v-model="form.fctMealsVOS[index].mealType" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>


                <el-button class="btn"  @click="delMeal(index)" :style="{margin: '0 0 0 10px' }" type="danger">删除</el-button>
                <el-button class="btn" v-if="index === form.fctMealsVOS.length - 1 " @click="addMeal" type="primary">添加</el-button>                
            </div>
        </el-form-item>

        
      </el-form>

      <div slot="footer">
        <el-button type="primary" @click="submitMeal">确 定</el-button>
        <el-button @click="tocancel">取 消</el-button>
      </div>
    </el-dialog>    
  </div>
</template>

<script>
import { listDetl, getDetl, delDetl, addDetl, updateDetl } from "@/api/food/day";
import { listInfo } from "@/api/food/meals";

export default {
  name: "Detl",
  dicts: ['food_day_type','sys_symptom_type'],
  data() {
    return {
      mealList: [],

      mealopen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 一天膳食食谱详情表格数据
      detlList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fctDayMealId: null,
        mealType: null,
        mealsId: null,
        name: null,
        status: null,
        orderNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fctDayMealId: [
          { required: true, message: "天数的id不能为空", trigger: "blur" }
        ],
        mealsId: [
          { required: true, message: "膳食id不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
      },
        options: [{
          value: '0',
          label: '早餐'
        }, {
          value: '1',
          label: '中餐'
        }, {
          value: '2',
          label: '晚餐'
        }
        ],      
    };
  },
  created() {
    this.getList();
    listInfo(
        {
          pageNum: 1,
          pageSize: 2000,
        }
      ).then(response => {
          this.mealList = response.rows;
          this.loading = false;
          console.log('static222',this.mealList);
      });      
    },
  methods: {
    getMealName(e){
      console.log('static',e);
    },
    delMeal(index){
        this.form.fctMealsVOS.splice(index,1);
    },    
    addMeal(){
        this.form.fctMealsVOS.push({
            "mealsId": "",
            "name": "测试"
        })
    },      
    remoteMethodFront(query, item) {
        if (query !== '') {
          listInfo({
            name: query,
            pageNum: 1,
            pageSize: 2000,
          }).then(response => {
              this.mealList = response.rows;

            });

        } else {
          this.mealList = [];
        }
      },     

    handleAddMeals(row) {
      this.reset();
      const id = row.id || this.ids
      getDetl(id).then(response => {
        this.form = response.data;
        this.mealopen = true;
        this.title = "菜品添加";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDetl(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改一天膳食食谱详情";
      });
    },    
    submitMeal(){
          if (this.form.id != null) {
            updateDetl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.mealopen = false;
              this.getList();
            });
          }      
    },
    tocancel(){
      this.mealopen = false;
      this.reset();      
    },
    /** 查询一天膳食食谱详情列表 */
    getList() {
      this.loading = true;
      listDetl(this.queryParams).then(response => {
        this.detlList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fctDayMealId: null,
        mealType: null,
        mealsId: null,
        name: null,
        remark: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        orderNum: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加一天膳食食谱详情";
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDetl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetl(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除一天膳食食谱详情编号为"' + ids + '"的数据项？').then(function() {
        return delDetl(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/detl/export', {
        ...this.queryParams
      }, `detl_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
